package servlet;

import pojo.ClassicFormula;
import pojo.Tcm;
import service.ClassicFormulaService;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 *
 * @author lzj
 *
 */
@WebServlet(urlPatterns = "/CFServlet")
public class CFServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ClassicFormulaService service =new ClassicFormulaService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if("list".equals(action)) { // 管理页显示学生列表
            int curPage=Integer.parseInt(request.getParameter("curPage"));
            request.setAttribute("curPage", curPage);
            PageUtil<Tcm> pageUtil= service.getClassicFormulaList(curPage);
            request.setAttribute("cFList",pageUtil);
            request.getRequestDispatcher("/WEB-INF/jsp/CFList.jsp?curPage=1").forward(request, response);


        }
        else if("search".equals(action)){
            String key=request.getParameter("key");
            int flag=Integer.parseInt(request.getParameter("flag"));
            PageUtil pageUtil=service.getQueryList(key,flag);
            request.setAttribute("cFList", pageUtil);
            request.setAttribute("curPage", 1);
            request.getRequestDispatcher("/WEB-INF/jsp/CFList.jsp?curPage=1").forward(request, response);

        }
        else if("detail".equals(action)){
            int id=Integer.parseInt(request.getParameter("id"));
            request.setAttribute("detail",service.getDetail(id));
            request.getRequestDispatcher("/WEB-INF/jsp/CFDetail.jsp").forward(request, response);
        }
//        else if("editUI".equals(action)) {
//            Tcm tcm=service.getCurTcm(Integer.parseInt(request.getParameter("id")));
//            request.setAttribute("curTcm",tcm);
//            request.getRequestDispatcher("/WEB-INF/jsp/TcmEdit.jsp"). forward(request, response);
//        }
//        else if("editProc".equals(action)) {
//            Tcm tcm = new Tcm();
//            tcm.setId(Integer.parseInt(request.getParameter("id")));
//            service.updateTcm(autoWiredTcm(request,tcm));
//            response.sendRedirect("TcmServlet?action=list");
//        }
//        else if("addUI".equals(action)) {
//            request.getRequestDispatcher("/WEB-INF/jsp/TcmAdd.jsp"). forward(request, response);
//        }
//        else if("addProc".equals(action)) {
//            Tcm tcm = new Tcm();
//            service.addTcm(autoWiredTcm(request,tcm));
//            response.sendRedirect("TcmServlet?action=list");
//        }



    }


}
